<?php
namespace app\admin\controller;
use app\admin\logic\ModularLogic;

/**
 * 模块api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/18
 * Time: 15:09
 */
class Modular extends Base {

    public function __construct(){
        parent::__construct();
        $this->m_logic = new ModularLogic();
    }

    /**
     * 获取模块
     * @return \think\response\Json
     */
    public function ajax_get_modular(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->m_logic->get_search_modular_maps($params['branchid'],$params['floorid'],$params['regionid']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('modular')->get_list($maps,'id,regionid,name,sort','sort asc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //查找区域
            $r_maps = [];
            $r_maps[] = ['id','=',$v['regionid']];
            $r_detail = model('region')->get_detail($r_maps,'id,floorid,name');
            $list[$k]['region_zw'] = $r_detail['name'];

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$r_detail['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
            $list[$k]['floorid'] = $f_detail['id'];
            $list[$k]['floor_zw'] = $f_detail['name'];

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $list[$k]['branchid'] = $b_detail['id'];
            $list[$k]['branch_zw'] = $b_detail['name'];
        }

        $total_num = model('modular')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->m_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增模块
     * @return \think\response\Json
     */
    public function ajax_add_modular(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查新增模块规则
        $r = $this->m_logic->check_add_modular_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $m_list = $params['m_data'];
        $m_data = [];
        foreach($m_list as $k => $v){
            $m_data[$k]['regionid'] = $params['regionid'];
            $m_data[$k]['name'] = $v['name'];
        }

        $r = model('modular')->insertAll($m_data);
        if($r){
            return json($this->m_logic->get_response(1,'新增成功'));
        }
        return json($this->m_logic->get_response(0,'新增失败'));
    }

    /**
     * 修改模块
     * @return \think\response\Json
     */
    public function ajax_save_modular(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改模块规则
        $r = $this->m_logic->check_save_modular_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $m_detail = $r['data']['m_detail'];

        $m_data = [];
        $m_data['regionid'] = $params['regionid'];
        $m_data['name'] = trim($params['name']);

        $maps = [];
        $maps[] = ['id','=',$m_detail['id']];
        $r = model('modular')->force()->save($m_data,$maps);
        if($r !== false){
            return json($this->m_logic->get_response(1,'修改成功'));
        }
        return json($this->m_logic->get_response(0,'修改失败'));
    }

    /**
     * 删除模块
     * @return \think\response\Json
     */
    public function ajax_del_modular(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $r = model('modular')->destroy($params['modularid']);
        if($r){
            return json($this->m_logic->get_response(1,'删除成功'));
        }
        return json($this->m_logic->get_response(0,'删除失败'));
    }
}